package com.sinocare.project.report.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinocare.project.common.entity.SccDrugstore;
import com.sinocare.project.common.entity.SccPatient;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.report.dao.SccDrugstoreDao;
import com.sinocare.project.report.db.SccPatientDo;
import com.sinocare.project.report.service.SccDrugstoreService;
import com.sinocare.project.report.service.SccPatientService;
import com.sinocare.project.report.vo.SccPatientVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 检测者(用户/客户)表(SccPatient)表控制层
 *
 * @author makejava
 * @since 2021-12-21 15:53:05
 */
@RestController
@RequestMapping("sccPatient")
@CrossOrigin
public class SccPatientController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SccPatientService sccPatientService;

    @Resource
    private SccDrugstoreService sccDrugstoreService;

    /**
     * 分页查询所有数据
     *
     * @param page  当店页
     * @param limit 每页条数
     * @param data  查询实体json
     * @return 所有数据
     */
    @GetMapping
    public Result selectAll(Integer page, Integer limit, String data) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)){
            SccPatientVo vo = JSON.parseObject(data, SccPatientVo.class);
            wrapper.likeRight(!StrUtil.hasBlank(vo.getName()),"a.name",vo.getName());
            wrapper.likeRight(!StrUtil.hasBlank(vo.getPhone()),"a.phone",vo.getPhone());
            wrapper.eq(!StrUtil.hasBlank(vo.getMainStore()),"c.parent_code",vo.getMainStore());
            wrapper.eq(!StrUtil.hasBlank(vo.getSubStore()),"c.store_code",vo.getSubStore());
        }
        PageHelper.startPage(page, limit);
        List<SccPatientDo> testIndex = this.sccPatientService.getTestIndex(wrapper);
        PageInfo<SccPatientDo> info = new PageInfo<>(testIndex);
        return Result.ok(ResultCode.SUCCESS, info.getTotal(), info.getList());
    }

    @GetMapping("mainStore")
    public Result selectMainStore() {
        QueryWrapper<SccDrugstore> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_code", 0);
        return Result.ok(ResultCode.SUCCESS, this.sccDrugstoreService.list(wrapper));
    }

    @GetMapping("subStore")
    public Result selectSubStore(String mainStoreCode) {
        QueryWrapper<SccDrugstore> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_code", mainStoreCode);
        return Result.ok(ResultCode.SUCCESS, this.sccDrugstoreService.list(wrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return Result.ok(ResultCode.SUCCESS, this.sccPatientService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sccPatient 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody SccPatient sccPatient) {
        return Result.ok(ResultCode.SUCCESS, this.sccPatientService.save(sccPatient));
    }

    /**
     * 修改数据
     *
     * @param sccPatient 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody SccPatient sccPatient) {
        return Result.ok(ResultCode.SUCCESS, this.sccPatientService.updateById(sccPatient));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(ResultCode.SUCCESS, this.sccPatientService.removeByIds(idList));
    }
}

